// pages/recharge/recharge.js

import { createStoreBindings } from 'mobx-miniprogram-bindings';
import { store } from '../../store/store';
import Toast from '@vant/weapp/toast/toast';
import Dialog from '@vant/weapp/dialog/dialog';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    columns: [1, 2, 3, 4, 5, 10, 15, 20, 50, 100],
    money: 1,
    urlRecharge: "/portal/user/recharge"
  },

  onChange(event) {
    const { picker, value, index } = event.detail;
    this.setData({
      money: value
    })
    Toast(`当前值：${value}元`);
    console.log(value);
  },

  pay() {
    Dialog.confirm({
      message: '确认支付'+this.data.money+'元',
    })
      .then(() => {
        // on confirm
        Toast.loading({
          message: '加载中...',
          forbidClick: true,
          loadingType: 'spinner',
        });

        wx.request({
          url: this.data.urlRecharge,
          method: "POST",
          data: {
            userId: this.getUser().id,
            money: this.data.money,
          },
          success: (res) => {
            console.log(res);
            if (res.data.code == 20012) {
              Toast.success(res.data.msg);
              wx.navigateBack();
            } else {
              Toast.fail(res.data.msg);
            }
          },
          fail: (res) => {
            console.log(res);
          },
          complete: () => {}
        })
      })
      .catch(() => {
        // on cancel
      });
    
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 挂载全局数据
    this.storeBindings = createStoreBindings(this, {
      store,
      fields: ['user'],
      actions: ['setUser', 'getUser', 'getWebsite']
    });

    this.setData({
      urlRecharge: this.getWebsite() + this.data.urlRecharge,
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // 销毁挂载绑定
    this.storeBindings.destroyStoreBindings();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})